package cool.houge.pig.service.auth;

/** @author KK (kzou227@qq.com) */
public final class AuthTokenException extends RuntimeException {

  private final Error error;

  /** @param error */
  public AuthTokenException(Error error, String message) {
    super(message);
    this.error = error;
  }

  /** @return */
  public Error getError() {
    return error;
  }

  /**
   * <a
   * href="https://datatracker.ietf.org/doc/html/rfc6749">https://datatracker.ietf.org/doc/html/rfc6749</a>.
   *
   * @author KK (kzou227@qq.com)
   */
  public enum Error {

    /**
     * The request is missing a parameter so the server can’t proceed with the request. This may
     * also be returned if the request includes an unsupported parameter or repeats a parameter.
     */
    INVALID_REQUEST("invalid_request", 400),
    /**
     * Client authentication failed, such as if the request contains an invalid client ID or secret.
     * Send an HTTP 401 response in this case.
     */
    INVALID_CLIENT("invalid_client", 401),
    /**
     * The authorization code (or user’s password for the password grant type) is invalid or
     * expired. This is also the error you would return if the redirect URL given in the
     * authorization grant does not match the URL provided in this access token request.
     */
    INVALID_GRANT("invalid_grant", 400),
    /**
     * This client is not authorized to use the requested grant type. For example, if you restrict
     * which applications can use the Implicit grant, you would return this error for the other
     * apps.
     */
    UNAUTHORIZED_CLIENT("unauthorized_client", 400),
    /**
     * If a grant type is requested that the authorization server doesn’t recognize, use this code.
     * Note that unknown grant types also use this specific error code rather than using the
     * invalid_request above.
     */
    UNSUPPORTED_GRANT_TYPE("unsupported_grant_type", 400),
    /** 认证失败. */
    AUTHENTICATION_FAILED("authentication_failed", 400),
    ;

    private final String error;
    private final int httpStatus;

    Error(String error, int httpStatus) {
      this.error = error;
      this.httpStatus = httpStatus;
    }

    public String getError() {
      return error;
    }

    public int getHttpStatus() {
      return httpStatus;
    }
  }
}
